package com.we823.cms.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.we823.cms.common.web.pageable.Direction;
import com.we823.cms.common.web.pageable.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.FrontUtils;
import com.we823.cms.model.extension.CaseCategory;
import com.we823.cms.model.extension.CaseModel;
import com.we823.cms.repository.service.extension.CaseCategoryService;
import com.we823.cms.repository.service.extension.CaseService;

@Controller
public class CaseController {

	private String baseUrl = WebConstants.WEB_TEMPLATE_PREFIX;
	
	private @Resource CaseService caseService;
	private @Resource CaseCategoryService caseCategoryService;
	
	@RequestMapping("cases")
	public String cases(HttpServletRequest request,Model model){
		Integer id = RequestUtil.getValueByName(request, "id", -1);
		model.addAttribute("category_id",id);
		String categoryName = "所有案例";
		if(id>0){
			CaseCategory caseCategoryModel = caseCategoryService.findOne(id);
			if(caseCategoryModel!=null){
				categoryName = caseCategoryModel.getName();
			}
		}
		model.addAttribute("category_name",categoryName);
		return FrontUtils.buildTemplateUrl(baseUrl, "cases");
	}
	@RequestMapping("case_{id}")
	public String caseGet(@PathVariable("id")Integer id,Model model){
		
		CaseModel caseModel = caseService.findOne(id);
		model.addAttribute("caseModel",caseModel);
		
		List<CaseModel> otherCaseModels = caseService.findByCategory(caseModel.getCategory(), new Sort(Direction.DESC,"addtime"));
		if(otherCaseModels!=null && otherCaseModels.size()>1 ){
			model.addAttribute("show","list");
			model.addAttribute("otherCases",otherCaseModels);
		}else{
			model.addAttribute("show","category");
		}
		return FrontUtils.buildTemplateUrl(baseUrl, "case");
	}
	@RequestMapping("cases_get")
	@ResponseBody
	public Model gets(HttpServletRequest request,Model model){
		Integer categoryId = RequestUtil.getValueByName(request, "category_id", 0);
		List<CaseModel> caseModels = null;
		
		Sort sort = new Sort(Direction.DESC,"addtime");
		
		if(categoryId==0){
			caseModels = caseService.findAll(sort);
		}else{
			CaseCategory caseCategoryModel = caseCategoryService.findOne(categoryId);
			caseModels = caseService.findByCategory(caseCategoryModel,sort);
		}
		
		if(caseModels!=null && caseModels.size()>0){
			if(caseModels.size()==1){
				model.addAttribute("show","content");
				model.addAttribute("case",caseModels.get(0));
			}else{
				model.addAttribute("show","list");
				model.addAttribute("cases",caseModels);
			}
		}else{
			model.addAttribute("show","empty");
			model.addAttribute("cases",caseModels);
		}
		return model;
	}
	
}
